package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.properties.AliOssProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private AliOssProperties aliOssProperties;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/page")
    Result getPage(DishPageQueryDTO queryDTO) {
        PageResult pageResult = new PageResult();
        pageResult = dishService.getPage(queryDTO);
        return Result.success(pageResult);
    }

    @PutMapping
    Result update(@RequestBody DishDTO dishDTO) {
        //删除缓存
        cleanCache("dish_*");

        dishService.update(dishDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    Result updateStatus(@PathVariable("status") Integer status,long id) {
        //删除缓存
        cleanCache("dish_*");
        DishDTO dishDTO = new DishDTO();
        dishDTO.setStatus(status);
        dishDTO.setId(id);
        dishService.updateStatus(dishDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    Result getById(@PathVariable("id") Long id) {

        DishVO dishVO= dishService.getDishById(id);
        return Result.success(dishVO);
    }

    @DeleteMapping()
    Result delete(String ids) {
        //删除缓存
        cleanCache("dish_*");
        //把字符串转为数字后进行删除
       int[] arr =  Arrays.stream(ids.split(","))
                .mapToInt(Integer::parseInt)  // 将每个字符串元素转换为整数
                .toArray();
       //arr转为long
        long[] arr1 = Arrays.stream(arr).asLongStream().toArray();
       dishService.delete(arr1);

        return Result.success();
    }

    @PostMapping()
    public Result addDish(@RequestBody DishDTO dishDTO) {
        //清理指定分类id的缓存
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);
        dishService.insertDish(dishDTO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(long categoryId) {
        //根据分类id查询菜品
        Dish dish = new Dish();
        dish.setStatus(StatusConstant.ENABLE);
        dish.setCategoryId(categoryId);
        List<DishVO> dishVOList = dishService.listWithFlavor(dish);
        return Result.success(dishVOList);
    }

    //清理缓存方法
    public void cleanCache(String pattern){
        redisTemplate.delete(redisTemplate.keys(pattern));
    }

}
